package sample

import (
	"organoid_public/domain/sample"
	sampleService "organoid_public/domain/sample/service"
	"organoid_public/global"
	"organoid_public/pkg/logger"
)

type service struct {
	log             logger.Logger
	protocolService sampleService.ProtocolServiceInterface
}

func newService(log logger.Logger) (*service, error) {
	uc, err := sample.Service()
	if err != nil {
		return nil, err
	}
	return &service{
		log:             global.Log,
		protocolService: uc.ProtocolSvc,
	}, nil
}

func (s *service) GetProtocolDetail(params *SampleIdReq) (*ProtocolDetailRes, error) {
	processList, mediumList, keyNodeList, materialList, err := s.protocolService.ProtocolDetail(params.SampleId)
	if err != nil {
		return nil, err
	}
	material := make([]Material, 0, len(materialList))
	for _, m := range materialList {
		material = append(material, Material{
			Name:        m.MaterialName,
			Description: m.Description,
		})
	}
	process := make([]Process, 0, len(processList))
	for _, p := range processList {
		process = append(process, Process{
			ProcessName: p.ProcessName,
			Start:       p.StartDay,
			End:         p.EndDay,
			Description: p.Description,
		})
	}
	medium := make([]Medium, 0, len(mediumList))
	for _, m := range mediumList {
		medium = append(medium, Medium{
			Name:  m.MediumName,
			Start: m.StartDay,
			End:   m.EndDay,
		})
	}
	keyNode := make([]KeyNode, 0, len(keyNodeList))
	for _, k := range keyNodeList {
		keyNode = append(keyNode, KeyNode{
			Name: k.KeyNodeName,
			Day:  k.Day,
		})
	}

	res := ProtocolDetailRes{
		Process:  process,
		Medium:   medium,
		Material: material,
		KeyNode:  keyNode,
	}
	return &res, nil
}
